﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BySoft.Server.Common.Enum;
using BySoft.Server.Business;
using BySoft.Web.Utility;
using BySoft.Server.Common.Entity;
using BySoft.Server.Consts;
using BySoft.Server.Common;
using System.Data;
using OneSoft.EntityCore;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;
using System.Text;

namespace BySoft.Web.Site.ComPage
{
    public partial class SetKnifeReturn : BasePopPage
    {
        #region 变量定义
         #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitDDL();
                InitData();
            }
        }

        void InitDDL()
        {
            
            DataTable dt = BsKnifeuse.SelectReturnUser();
            if (dt.Rows.Count > 0)
            {
                this.ddlUserID.DataSource = dt;
                this.ddlUserID.DataTextField = "UserName";
                this.ddlUserID.DataValueField = "UserID";
                this.ddlUserID.DataBind();
                UIHelper.InsertFirstItem(ddlUserID);

               
           
            }
        }
        void InitData()
        {


            hidDetailID.Value = HttpUtility.UrlDecode(UIHelper.GetQueryStringValue("id"));
           
        }


      
        protected void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                if (!ValidateData())
                {
                    return;
                }

                string billID = hidDetailID.Value;

                BsKnifeusedetailEntity entity = BsKnifeusedetail.GetBsKnifeusedetailByID(Convert.ToDecimal(billID));

                entity.ReturnUserID = ddlUserID.SelectedValue;
                var date = CommonMethod.GetStartTime(cpReturnDate.Value);

                entity.ReturnDate = DateTime.Parse(date);
                BsKnifeusedetail.UpdateBsKnifeusedetail(entity);

                                     
                string sReturn = "success";
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>setCookie(dialogReturn_key,'" + sReturn + "',1);CloseTopDialog();</script>");
            }
            catch (Exception ex)
            {
                UIHelper.AlertMessage(this.Page, ex.Message.ToString());
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            try
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>CloseTopDialog();</script>");
            }
            catch (Exception ex)
            {
                UIHelper.Alert(this.Page, ex.ToString().Trim());
            }

        }

        private bool ValidateData()
        {

            if (string.IsNullOrEmpty(ddlUserID.SelectedValue))
            {
                UIHelper.AlertMessage(this.Page, "请选择归还人员！");
                return false;
            }

            var date = CommonMethod.GetStartTime(cpReturnDate.Value);

            if (string.IsNullOrEmpty(date))
            {
                UIHelper.AlertMessage(this.Page, "请选择归还日期！");
                return false;
            }
           

            return true;
        }
    }
}